package com.itheima.health;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;

public class testController {

   @Test
    public void  testLogin() throws IOException {
        //创建一个Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpPost httpPost = new HttpPost("http://localhost:9002/user/login");


//        创建一个JSON格式的键值对
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username","admin");
        jsonObject.put("password","1234");

        //将JSON格式键值对转换成JSON字符串
        StringEntity stringEntity = new StringEntity(jsonObject.toString());
        stringEntity.setContentType("application/json");
        //设置字符串编码
        stringEntity.setContentEncoding("utf-8");
        //将字符串添加到路径请求中
        httpPost.setEntity(stringEntity);

        //使用http发给请求并接收相应
        CloseableHttpResponse execute = httpClient.execute(httpPost);
        //讲接受到的相应转成实体数据
        String string = execute.getEntity().toString();

       System.out.println(string);
                        //讲接受到的请求获取请求行和请求行中的状态码
        int statusCode = execute.getStatusLine().getStatusCode();
        System.out.println(statusCode);

        Assert.assertEquals(200,statusCode);
        //关闭资源
        execute.close();
        httpClient.close();
    }
}
